package com.aaa.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;



import com.aaa.dao.ProductDAO;
import com.aaa.query.PageQuery;
import com.aaa.util.DAOUtil;

public class ProductDAOImpl implements ProductDAO{
	
	
	
	
	@Override
	public Map<String, Object> getProductById(Integer id) {
		
		String sql = "select  * from product where pid = ?";
		
		List<Map<String, Object>> list = DAOUtil.executeQuery(sql, id);
		
		if(list.size()>0) {
			return list.get(0);
		}
		return null;
	}
	
	@Override
	public Map<String, Object> listProduct(Map<String, Object> args) {
		
		
		//1 鏌ヨ鏈夊灏戞潯鏁版嵁   pflag=0涓婃灦鐨勫晢鍝�
		StringBuilder  sb1 = new StringBuilder("select count(*) c from product where pflag=0");
		List<Map<String, Object>> list = DAOUtil.executeQuery( sb1.toString() );
		Long totalCount = (Long) list.get(0).get("c");//  list  [{c:50}]
		
		//1.5 鎴戜滑鍒嗛〉鏉′俊鎭氨鍙互璁＄畻鍑烘潵浜�
		Long pageNo =  Long.parseLong(( String )args.get("pageNo")   ) ;
		PageQuery query = new PageQuery(pageNo , totalCount);
		
		//2 鏌ヨ璇ラ〉鐨勬暟鎹�
		StringBuilder sb2 = new StringBuilder("select * from product where pflag=0 ");
		sb2.append( "  limit  ? , ?  ");// limit 璧峰绱㈠紩 锛屾暟閲�
		List<Map<String, Object>> list2 = DAOUtil.executeQuery(sb2.toString()  ,  (pageNo-1)*query.getCount() ,  query.getCount() );
		
		//3 灏嗘暟鎹鍒颁竴涓猰ap涓繑鍥�
		Map<String,Object> map = new HashMap<>();
		map.put( "pageInfo" , query);
		map.put( "pageData" , list2);
		
		return map;
	}

	@Override
	public Map<String, Object> queryProduct(Map<String, Object> args) {
		// select * from product where plage=0 and pname like '%asd%' and shopPrice > 25
		
		StringBuilder sql = new StringBuilder();
		//杩欑闈炵┖鍒ゆ柇 缁忓父浣跨敤 鎵�浠ュ紑鍙戠殑鏃跺�� 閮芥槸灏佽鎴愬伐鍏风被
		if(  args.get("productName") != null && !"".equals( args.get("productName") )  ) {
			sql.append(    "   and  pname  like '%"+args.get("productName")+"%'  " );
		}
		if( args.get("minPrice") !=null && !"".equals( args.get("minPrice") ) ) {
			sql.append("   and  shopPrice  >  "  +   args.get("minPrice")    );
		}
		if( args.get("maxPrice") !=null && !"".equals( args.get("maxPrice") )   ) {
			sql.append("   and  shopPrice  <  "  +   args.get("maxPrice")    );
		}
		if( args.get("isHot") !=null && !"".equals( args.get("isHot") )   ) {
			sql.append("   and  isHot =  "  +   args.get("isHot")    );
		}
		if( args.get("price") !=null && !"".equals( args.get("price") )   ) {
			sql.append("  order by  shopPrice "  +   args.get("price")    );
		}
		
		// 1 鏌ヨ 绗﹀悎 鏉′欢鐨勬暟鎹� 鎬绘暟閲�
		StringBuilder  sb1 = new StringBuilder();
		sb1.append("select  count(*) c  from product where pflag = 0 "   );
		sb1.append(  sql );
		
		List<Map<String, Object>> list = DAOUtil.executeQuery(sb1.toString()); //[{c=50}] 
		Long totalCount = (Long) list.get(0).get("c");
		
		Long pageNo = Long.parseLong((String)args.get("pageNo"));
		PageQuery pageQuery = new PageQuery(pageNo, totalCount);//鍒嗛〉鏉′俊鎭暣鐞嗗畬鎴�
		// 2 鏌ヨ绗﹀悎鏉′欢鐨� 褰撳墠椤垫暟鎹�
		StringBuilder sb2 = new StringBuilder();
		sb2.append("select *  from product where pflag = 0 ");
		sb2.append(sql);
		sb2.append("   limit ? , ? "); //鎴戜滑浠ュ悗涓嶄娇鐢� DAOUtil  鑰屾槸浣跨敤 mybatis妗嗘灦 閲岄潰鏈夊垎椤垫彃浠�
		
		System.out.println(sb2);
		
		List<Map<String, Object>> list2 = DAOUtil.executeQuery(sb2.toString(),  (pageNo -1)* pageQuery.getCount() ,pageQuery.getCount());
		
		// 3 鏁寸悊鏁版嵁骞惰繑鍥�
		Map<String,Object> map = new HashMap<>();
		
		map.put("pageInfo", pageQuery);
		map.put("pageData", list2 );
		
		
		return map;
	}

	
	

}
